<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="计算机,全文检索,lucene," />





  <link rel="alternate" href="/atom.xml" title="学不可以已" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/uploads/favicon.ico?v=5.1.2" />






<meta name="description" content="LUCENE是什么 Lucene是一款高性能的，可扩展的信息检索（IR)工具库，信息检索是指文档搜索，文档内信息搜索或者文档相关元数据搜索操作等，Lucene能够融入到你的应用程序当中，以增加搜索功能。它是一款以java实现的成熟，自由，开源的软件项目，也是apache软件基金中的一个项目，并且基于apache软件许可协议授权。因此，lucene在近年来已经成为最受欢迎的开源信息检索工具库。 Lu">
<meta name="keywords" content="计算机,全文检索,lucene">
<meta property="og:type" content="article">
<meta property="og:title" content="Lucene核心API">
<meta property="og:url" content="http://eregg.com/2016/11/22/Lucene核心API/index.html">
<meta property="og:site_name" content="学不可以已">
<meta property="og:description" content="LUCENE是什么 Lucene是一款高性能的，可扩展的信息检索（IR)工具库，信息检索是指文档搜索，文档内信息搜索或者文档相关元数据搜索操作等，Lucene能够融入到你的应用程序当中，以增加搜索功能。它是一款以java实现的成熟，自由，开源的软件项目，也是apache软件基金中的一个项目，并且基于apache软件许可协议授权。因此，lucene在近年来已经成为最受欢迎的开源信息检索工具库。 Lu">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-09-21T18:14:58.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Lucene核心API">
<meta name="twitter:description" content="LUCENE是什么 Lucene是一款高性能的，可扩展的信息检索（IR)工具库，信息检索是指文档搜索，文档内信息搜索或者文档相关元数据搜索操作等，Lucene能够融入到你的应用程序当中，以增加搜索功能。它是一款以java实现的成熟，自由，开源的软件项目，也是apache软件基金中的一个项目，并且基于apache软件许可协议授权。因此，lucene在近年来已经成为最受欢迎的开源信息检索工具库。 Lu">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.2',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://eregg.com/2016/11/22/Lucene核心API/"/>





  <title>Lucene核心API | 学不可以已</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?5f1629a8f7280df347a90a49b56b2b49";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">学不可以已</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">青，取之于蓝，而青于蓝</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://eregg.com/2016/11/22/Lucene核心API/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="eregg">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学不可以已">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Lucene核心API</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2016-11-22T09:41:08+08:00">
                2016-11-22
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h4 id="LUCENE是什么"><a href="#LUCENE是什么" class="headerlink" title="LUCENE是什么"></a><a href="#LUCENE是什么" title="LUCENE是什么"></a>LUCENE是什么</h4><blockquote>
<p><strong>Lucene是一款高性能的，可扩展的信息检索（IR)工具库</strong>，信息检索是指文档搜索，文档内信息搜索或者文档相关元数据搜索操作等，Lucene能够融入到你的应用程序当中，以增加搜索功能。它是一款以java实现的成熟，自由，开源的软件项目，也是apache软件基金中的一个项目，并且基于apache软件许可协议授权。因此，lucene在近年来已经成为最受欢迎的开源信息检索工具库。</p>
<p>Lucene是一个快速发展的项目。</p>
</blockquote>
<h4 id="Lucene-和搜索程序组建"><a href="#Lucene-和搜索程序组建" class="headerlink" title="Lucene 和搜索程序组建"></a><a href="#Lucene-和搜索程序组建" title="Lucene 和搜索程序组建"></a>Lucene 和搜索程序组建</h4><blockquote>
<p><strong>检索原始内容；将原始内容，封装到Field对象，在将Field对象添加到Docuemnt对象，然后将Document对象写入索引库，一旦建立索引，用于搜索的组建也就出来了，这些搜索的组建包括：用户接口，构建可编程查询语句的方法，执行查询语句，展现查询结果等</strong>。</p>
</blockquote>
<h4 id="索引过程的核心类"><a href="#索引过程的核心类" class="headerlink" title="索引过程的核心类"></a><a href="#索引过程的核心类" title="索引过程的核心类"></a>索引过程的核心类</h4><figure class="highlight xl"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">graph LR</div><div class="line"><span class="function"><span class="title">filed</span>–&gt;</span>Document</div><div class="line"><span class="function"><span class="title">filed</span>–&gt;</span>Document</div><div class="line"><span class="function"><span class="title">filed</span>–&gt;</span>Document</div><div class="line">D<span class="function"><span class="title">ocument</span>–&gt;</span>Analyzer</div><div class="line">A<span class="function"><span class="title">nalyzer</span>–&gt;</span>indexWriter</div><div class="line"><span class="function"><span class="title">indexWriter</span>–&gt;</span>Directory</div></pre></td></tr></table></figure>

<h5 id="IndexWriter"><a href="#IndexWriter" class="headerlink" title="IndexWriter"></a><a href="#IndexWriter" title="IndexWriter"></a>IndexWriter</h5><blockquote>
<p><strong>IndexWriter(写索引)是索引过程的核心组建。这个类负责创建新索引或者打开已有的索引，以及向索引中添加，删除或者更新被索引的文档信息</strong>。可以把IndexWriter看做这样一个对象：它为你提供针对索引文件的写入操作，但不能用于读取或搜索索引。IndexWriter需要开辟一定的空间来存储索引，该功能可以由Directory完成。</p>
</blockquote>
<h5 id="Directory"><a href="#Directory" class="headerlink" title="Directory"></a><a href="#Directory" title="Directory"></a>Directory</h5><blockquote>
<p><strong>Directory 类描述了Lucene索引的存放位置</strong>。它是一个抽象类，它的子类负责具体指定索引的存储路径。可以存放在磁盘上（FSDirectory），也可以存放在内存中（RAMDirectory）等待。。</p>
</blockquote>
<h5 id="Analyzer"><a href="#Analyzer" class="headerlink" title="Analyzer"></a><a href="#Analyzer" title="Analyzer"></a>Analyzer</h5><blockquote>
<p>文本文件在被索引之前，需要经过Analyzer(分词器)处理。<strong>它负责从被索引文本文件中提取语汇单元，并剔除剩下的无用信息</strong>。分词器主要分析的对象是Document对象，该文档对象包含了一些分离的能被索引的域。</p>
</blockquote>
<h5 id="Document"><a href="#Document" class="headerlink" title="Document"></a><a href="#Document" title="Document"></a>Document</h5><blockquote>
<p><strong>Document(文档)对象代表一些域（Field）的集合</strong>。可以将Document对象理解为虚拟文档——比如web页面，Email信息或者文本文件————然后从中取回大量元数据（如：作者，标题，摘要，URL……），这作为文档的不同域单独存储并被索引。</p>
</blockquote>
<h5 id="Field"><a href="#Field" class="headerlink" title="Field"></a><a href="#Field" title="Field"></a>Field</h5><blockquote>
<p><strong>索引中的每个文档都包含一个或多个不同命名的域，这些域包含在Field类中。每个域都有一个域名和对应的域值，以及一组选项来精准控制Lucene索引操作的各个域值</strong>。</p>
<table><br><thead><br><tr><br><th>Field类</th><br><th>数据类型</th><br><th>Analyzed是否分析</th><br><th>Indexed是否索引</th><br><th>Stored是否存储</th><br><th>说明</th><br></tr><br></thead><br><tbody><br><tr><br><td>StringField(FieldName, FieldValue,Store.YES))</td><br><td>字符串</td><br><td>N</td><br><td>Y</td><br><td>Y or N</td><br><td>这个Field用来构建一个字符串Field，但是不会进行分析，会将整个串存储在索引中，比如(订单号,姓名等)是否存储在文档中用Store.YES或Store.NO决定</td><br></tr><br><tr><br><td>LongField(FieldName, FieldValue,Store.YES)</td><br><td>Long型</td><br><td>Y</td><br><td>Y</td><br><td>Y or N</td><br><td>这个Field用来构建一个Long数字型Field，进行分析和索引，比如(价格)是否存储在文档中用Store.YES或Store.NO决定</td><br></tr><br><tr><br><td>StoredField(FieldName, FieldValue)</td><br><td>重载方法，支持多种类型</td><br><td>N</td><br><td>N</td><br><td>Y</td><br><td>这个Field用来构建不同类型Field不分析，不索引，但要Field存储在文档中。</td><br></tr><br><tr><br><td>TextField(FieldName, FieldValue, Store.NO)或TextField(FieldName, reader)</td><br><td>字符串或流</td><br><td>Y</td><br><td>Y</td><br><td>Y or N</td><br><td>如果是一个Reader,lucene猜测内容比较多,会采用Unstored的策略</td><br></tr><br></tbody><br></table>

</blockquote>
<h4 id="搜索过程的核心类"><a href="#搜索过程的核心类" class="headerlink" title="搜索过程的核心类"></a><a href="#搜索过程的核心类" title="搜索过程的核心类"></a>搜索过程的核心类</h4><h5 id="IndexSearcher"><a href="#IndexSearcher" class="headerlink" title="IndexSearcher"></a><a href="#IndexSearcher" title="IndexSearcher"></a>IndexSearcher</h5><blockquote>
<p><strong>IndexSearcher 类用于搜素由IndexWriter类创建的索引</strong>：这个类公开几个搜索方法，它是连接索引的中心环节。可以将IndexSearcher类看做一个以只读方式打开索引的类。它需要利用Directory实例来掌控前期创建的索引，然后才能提供大量的搜索方法。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//该对象的典型应用如下</span></div><div class="line">IndexReader reader = DirectoryReader.open(FSDirectory.open(<span class="keyword">new</span> File(<span class="string">“/tmp/lucene/indexdata”</span>)));</div><div class="line"></div><div class="line">IndexSearcher searcher = <span class="keyword">new</span> IndexSearcher(reader);</div><div class="line"></div><div class="line">Query query = <span class="keyword">new</span> TermQuery(<span class="keyword">new</span> Term(<span class="string">“filename”</span>, <span class="string">“检索”</span>));</div><div class="line">TopDocs docs = searcher.search(query, <span class="number">10</span>);</div></pre></td></tr></table></figure>

</blockquote>
<h5 id="Term"><a href="#Term" class="headerlink" title="Term"></a><a href="#Term" title="Term"></a>Term</h5><blockquote>
<p><strong>Term对象是搜索功能的基本单元。与Field对象类似，Term对象包含一对字符串元素：域名和单词（或域文本值）。</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//在搜索过程中可以创建Term对象，并和QueryTerm对象一起使用</span></div><div class="line">Query query = <span class="keyword">new</span> TermQuery(<span class="keyword">new</span> Term(<span class="string">“filename”</span>, <span class="string">“检索”</span>));</div><div class="line">TopDocs docs = searcher.search(query, <span class="number">10</span>);</div></pre></td></tr></table></figure>

</blockquote>
<h5 id="Query"><a href="#Query" class="headerlink" title="Query"></a><a href="#Query" title="Query"></a>Query</h5><blockquote>
<p>Lucene含有许多具体的Query(查询)子类。</p>
</blockquote>
<h6 id="TermQuery-通过项进行搜索"><a href="#TermQuery-通过项进行搜索" class="headerlink" title="TermQuery 通过项进行搜索"></a><a href="#TermQuery-通过项进行搜索" title="TermQuery 通过项进行搜索"></a>TermQuery 通过项进行搜索</h6><blockquote>
<p><strong>TremQuery 是Lucene提供的最基本的查询类型，也是最简单的查询类型之一。它用来匹配指定域中包含特定值的文档</strong>。</p>
</blockquote>
<h6 id="TermRangeQuery-在指定的项范围内搜索"><a href="#TermRangeQuery-在指定的项范围内搜索" class="headerlink" title="TermRangeQuery 在指定的项范围内搜索"></a><a href="#TermRangeQuery-在指定的项范围内搜索" title="TermRangeQuery 在指定的项范围内搜索"></a>TermRangeQuery 在指定的项范围内搜索</h6><blockquote>
<p>索引中的各个Term对象会按照字典排序顺序进行排序，并允许在Lucene的TermRangeQuery对象提供的范围内进行文本项的直接搜索。</p>
</blockquote>
<h6 id="NumericRangeQuery-在指定的数字范围内搜索"><a href="#NumericRangeQuery-在指定的数字范围内搜索" class="headerlink" title="NumericRangeQuery 在指定的数字范围内搜索"></a><a href="#NumericRangeQuery-在指定的数字范围内搜索" title="NumericRangeQuery 在指定的数字范围内搜索"></a>NumericRangeQuery 在指定的数字范围内搜索</h6><blockquote>
<p>如果使用NumericField对象来索引域，那么就能使用NumericRangeQuery类在某个特定范围内搜索该域。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//数字范围查询</span></div><div class="line">Query query = NumericRangeQuery.newLongRange(<span class="string">“fileSize”</span>, <span class="number">0l</span>, <span class="number">1000l</span>, <span class="keyword">true</span>, <span class="keyword">true</span>);</div><div class="line"></div><div class="line"><span class="keyword">this</span>.search(booleanQuery);</div><div class="line">查询条件：fileSize:[<span class="number">0</span> TO <span class="number">1000</span>]</div></pre></td></tr></table></figure>

</blockquote>
<h6 id="PrefixQuery-通过字符串搜索"><a href="#PrefixQuery-通过字符串搜索" class="headerlink" title="PrefixQuery 通过字符串搜索"></a><a href="#PrefixQuery-通过字符串搜索" title="PrefixQuery 通过字符串搜索"></a>PrefixQuery 通过字符串搜索</h6><h6 id="FuzzyQuery-搜索类似项"><a href="#FuzzyQuery-搜索类似项" class="headerlink" title="FuzzyQuery 搜索类似项"></a><a href="#FuzzyQuery-搜索类似项" title="FuzzyQuery 搜索类似项"></a>FuzzyQuery 搜索类似项</h6><h6 id="MatchAllDocsQuery-匹配所有文档"><a href="#MatchAllDocsQuery-匹配所有文档" class="headerlink" title="MatchAllDocsQuery 匹配所有文档"></a><a href="#MatchAllDocsQuery-匹配所有文档" title="MatchAllDocsQuery 匹配所有文档"></a>MatchAllDocsQuery 匹配所有文档</h6><h6 id="WildcardQuery-通配符查询"><a href="#WildcardQuery-通配符查询" class="headerlink" title="WildcardQuery 通配符查询"></a><a href="#WildcardQuery-通配符查询" title="WildcardQuery 通配符查询"></a>WildcardQuery 通配符查询</h6><h6 id="BooleanQuery-组合查询"><a href="#BooleanQuery-组合查询" class="headerlink" title="BooleanQuery 组合查询"></a><a href="#BooleanQuery-组合查询" title="BooleanQuery 组合查询"></a>BooleanQuery 组合查询</h6><blockquote>
<p><strong>通过使用BooleanQuery类可以将所有Query子类，组合成复杂的查询方式</strong>，可以通过Occur来表示子句的逻辑“与”，逻辑“或” 或者逻辑“非”。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//组合查询（多条件组合查询）</span></div><div class="line">TermQuery query1 = <span class="keyword">new</span> TermQuery(<span class="keyword">new</span> Term(<span class="string">“filename”</span>, <span class="string">“apache”</span>));</div><div class="line">NumericRangeQuery&lt;Long&gt; query2 = NumericRangeQuery.newLongRange(<span class="string">“fileSize”</span>, <span class="number">0l</span>, <span class="number">1000l</span>, <span class="keyword">true</span>, <span class="keyword">true</span>);</div><div class="line">BooleanQuery booleanQuery = <span class="keyword">new</span> BooleanQuery();</div><div class="line"><span class="comment">//Occur.MUST:必须满足，Occur.MUST_NOT:必须不满足, Occur.SHOULD:或者的关系</span></div><div class="line">booleanQuery.add(query1, Occur.MUST);</div><div class="line">booleanQuery.add(query2, Occur.MUST);</div><div class="line"></div><div class="line"><span class="keyword">this</span>.search(booleanQuery);</div><div class="line">查询条件：+filename:apache +fileSize:[<span class="number">0</span> TO <span class="number">1000</span>]</div></pre></td></tr></table></figure>

</blockquote>
<h5 id="QueryParser-解析查询表达式"><a href="#QueryParser-解析查询表达式" class="headerlink" title="QueryParser 解析查询表达式"></a><a href="#QueryParser-解析查询表达式" title="QueryParser 解析查询表达式"></a>QueryParser 解析查询表达式</h5><blockquote>
<p>尽管基于API创建的查询对象很强大，但是如果所有的查询对象都必须通过java代码显式构造的话，这也是不合理的。通过使用自然语言表示的查询表达式，Lucene的QueryParser类可以创建前面介绍过的某一个Query子类。</p>
<p><strong>QueryParser 能够很好地为搜索用户提供形式自由的查询。为了完成这个任务，QueryParser 使用分词器将文本信息分割成各个项以用于搜索</strong>。在实例化QueryParser对象时，通用需要传入一个分词器对象。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/<em>*</em></span></div><div class="line">  参数一：默认搜索域，参数二：分词器</div><div class="line"> */</div><div class="line">QueryParser queryParser = <span class="keyword">new</span> QueryParser(<span class="string">“filename”</span>, <span class="keyword">new</span> IKAnalyzer());</div><div class="line"><span class="comment">//parse 中可以不指定域，默认使用创建QueryParser时，指定的默认域（filename））</span></div><div class="line">Query query = queryParser.parse(<span class="string">“fileContent:全文检索”</span>);</div><div class="line"></div><div class="line"><span class="keyword">this</span>.search(query);</div><div class="line">查询条件：fileContent:全文 fileContent:检索</div></pre></td></tr></table></figure>

</blockquote>
<h6 id="MultiFieldQueryParser-一次性搜索多个域"><a href="#MultiFieldQueryParser-一次性搜索多个域" class="headerlink" title="MultiFieldQueryParser 一次性搜索多个域"></a><a href="#MultiFieldQueryParser-一次性搜索多个域" title="MultiFieldQueryParser 一次性搜索多个域"></a>MultiFieldQueryParser 一次性搜索多个域</h6><blockquote>
<p><strong>MultiFieldQueryParser 是QueryParser的子类</strong>。它会在后台程序中实例化一个QueryParser对象，用来针对每个域进行查询表达式的解析，然后使用BooleanQuery将查询组合起来。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//多个默认域，查询解析器</span></div><div class="line">String [] fields = &#123;<span class="string">“filename”</span>, <span class="string">“fileContent”</span>&#125;;</div><div class="line"><span class="comment">//参数一：默认搜索域数组（可以指定多个默认搜索域） 参数2：解析查询短语的分词器</span></div><div class="line">MultiFieldQueryParser multiFieldQueryParser = <span class="keyword">new</span> MultiFieldQueryParser(fields, <span class="keyword">new</span> IKAnalyzer());</div><div class="line">Query query = multiFieldQueryParser.parse(<span class="string">“lucene is good”</span>);</div><div class="line"></div><div class="line"><span class="keyword">this</span>.search(query);</div><div class="line">查询条件：(filename:lucene fileContent:lucene) (filename:good fileContent:good)</div></pre></td></tr></table></figure><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//多个默认域，查询解析器,设置域的权重，从而影响排序结果 </span></div><div class="line">String [] fields = &#123;<span class="string">“filename”</span>, <span class="string">“fileContent”</span>&#125;;</div><div class="line">Map&lt;String, Float&gt; boosts = <span class="keyword">new</span> HashMap&lt;String, Float&gt;();</div><div class="line">boosts.put(<span class="string">“filename”</span>, <span class="number">1f</span>);</div><div class="line">boosts.put(<span class="string">“fileContent”</span>, <span class="number">10f</span>);</div><div class="line"><span class="comment">//参数一：默认搜索域数组（可以指定多个默认搜索域） 参数2：解析查询短语的分词器,参数3：设置域的权重值</span></div><div class="line">MultiFieldQueryParser multiFieldQueryParser = <span class="keyword">new</span> MultiFieldQueryParser(fields, <span class="keyword">new</span> IKAnalyzer(), boosts);</div><div class="line">Query query = multiFieldQueryParser.parse(<span class="string">“lucene is good”</span>);</div><div class="line"></div><div class="line"><span class="keyword">this</span>.search(query);</div><div class="line">查询条件：(filename:lucene fileContent:lucene^<span class="number">10.0</span>) (filename:good fileContent:good^<span class="number">10.0</span>)</div></pre></td></tr></table></figure>

</blockquote>
<h5 id="TopDocs"><a href="#TopDocs" class="headerlink" title="TopDocs"></a><a href="#TopDocs" title="TopDocs"></a>TopDocs</h5><blockquote>
<p><strong>TopDocs 类是一个简单的指针容器，指针一般指向前N个排名的搜索结果</strong>，搜索结果即匹配查询条件的文档。</p>
</blockquote>
<h4 id="实例一（建立索引）"><a href="#实例一（建立索引）" class="headerlink" title="实例一（建立索引）"></a><a href="#实例一（建立索引）" title="实例一（建立索引）"></a>实例一（建立索引）</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> com.eregg.lucene.demo;</div><div class="line"></div><div class="line"><span class="keyword">import</span> java.io.File;</div><div class="line"></div><div class="line"><span class="keyword">import</span> org.apache.commons.io.FileUtils;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.analysis.Analyzer;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.analysis.standard.StandardAnalyzer;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.document.Document;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.document.Field;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.document.Field.Store;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.document.LongField;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.document.StoredField;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.document.TextField;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.index.IndexWriter;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.index.IndexWriterConfig;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.store.Directory;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.store.FSDirectory;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.util.Version;</div><div class="line"><span class="keyword">import</span> org.junit.Test;</div><div class="line"><span class="keyword">import</span> org.wltea.analyzer.lucene.IKAnalyzer;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">IndexBuilderDemo</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testBuilderIndex</span><span class="params">()</span> <span class="keyword">throws</span> Exception</span>&#123;</div><div class="line">        </div><div class="line">        <span class="comment">//指定索引数据存放位置，可以存放在磁盘上，也可以存放在内存中</span></div><div class="line">        Directory directory = FSDirectory.open(<span class="keyword">new</span> File(<span class="string">“/tmp/lucene/indexdata”</span>));</div><div class="line">        </div><div class="line">        <span class="comment">//创建一个标准分词器</span></div><div class="line">        Analyzer analyzer = <span class="keyword">new</span> StandardAnalyzer();</div><div class="line">        analyzer = <span class="keyword">new</span> IKAnalyzer();</div><div class="line">        <span class="comment">//将分词器及版本分装到IndexWriter所需的参数对象中</span></div><div class="line">        IndexWriterConfig conf = <span class="keyword">new</span> IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);</div><div class="line">        </div><div class="line">        <span class="comment">//根据索引库存放的目录，及需要的参数（版本，分词器）创建一个创建索引的工具indexWriter</span></div><div class="line">        IndexWriter indexWriter = <span class="keyword">new</span> IndexWriter(directory, conf);</div><div class="line">        indexWriter.deleteAll();<span class="comment">//删除原来的数据</span></div><div class="line">        </div><div class="line">        <span class="comment">/<em>*</em></span></div><div class="line">          从原始数据中提取信息到document，然后用indexWriter写入到索引库（索引库，信息库）</div><div class="line">         */</div><div class="line">        File path = <span class="keyword">new</span> File(<span class="string">“/tmp/lucene/searchsource”</span>);</div><div class="line">        File [] files = path.listFiles();</div><div class="line">        String filename = <span class="keyword">null</span>;</div><div class="line">        String filePath = <span class="keyword">null</span>;</div><div class="line">        String fileContent = <span class="keyword">null</span>;</div><div class="line">        <span class="keyword">long</span> fileSize = <span class="number">0</span>;</div><div class="line">        </div><div class="line">        <span class="keyword">for</span> (File file : files) &#123;</div><div class="line">            <span class="comment">//1.从原始数据提取出来的信息</span></div><div class="line">            filename = file.getName();</div><div class="line">            filePath = file.getPath();</div><div class="line">            fileContent = FileUtils.readFileToString(file);</div><div class="line">            fileSize = FileUtils.sizeOf(file);</div><div class="line">            </div><div class="line">            <span class="comment">//2.将提取出来的信息封装到field对象中</span></div><div class="line">            Field filenameField = <span class="keyword">new</span> TextField(<span class="string">“filename”</span>, filename, Store.YES);</div><div class="line">            Field filePathField = <span class="keyword">new</span> StoredField(<span class="string">“filePath”</span>, filePath);</div><div class="line">            Field fileContentField = <span class="keyword">new</span> TextField(<span class="string">“fileContent”</span>, fileContent, Store.NO);</div><div class="line">            LongField fileSizeField = <span class="keyword">new</span> LongField(<span class="string">“fileSize”</span>, fileSize, Store.YES);</div><div class="line">            </div><div class="line">            Document doc = <span class="keyword">new</span> Document();</div><div class="line">            doc.add(filenameField);</div><div class="line">            doc.add(filePathField);</div><div class="line">            doc.add(fileContentField);</div><div class="line">            doc.add(fileSizeField);</div><div class="line">            </div><div class="line">            indexWriter.addDocument(doc);</div><div class="line">            </div><div class="line">            System.out.println(<span class="string">“filename:”</span>+filename+<span class="string">“,fileSize:”</span>+fileSize);</div><div class="line">        &#125;</div><div class="line">        </div><div class="line">        indexWriter.commit();</div><div class="line">        indexWriter.close();</div><div class="line">        </div><div class="line">        </div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>

<h4 id="实例二（搜索索引）"><a href="#实例二（搜索索引）" class="headerlink" title="实例二（搜索索引）"></a><a href="#实例二（搜索索引）" title="实例二（搜索索引）"></a>实例二（搜索索引）</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> com.eregg.lucene.demo;</div><div class="line"></div><div class="line"><span class="keyword">import</span> java.io.File;</div><div class="line"></div><div class="line"><span class="keyword">import</span> org.apache.lucene.document.Document;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.index.DirectoryReader;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.index.IndexReader;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.index.Term;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.search.IndexSearcher;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.search.Query;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.search.ScoreDoc;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.search.TermQuery;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.search.TopDocs;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.store.FSDirectory;</div><div class="line"><span class="keyword">import</span> org.junit.Test;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">IndexSearchDemo</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testSearch</span><span class="params">()</span> <span class="keyword">throws</span> Exception</span>&#123;</div><div class="line">        </div><div class="line">        <span class="comment">//通过Directory指定索引库所在位置</span></div><div class="line">        IndexReader reader = DirectoryReader.open(FSDirectory.open(<span class="keyword">new</span> File(<span class="string">“/tmp/lucene/indexdata”</span>)));</div><div class="line">        </div><div class="line">        <span class="comment">//创建一个查询对象</span></div><div class="line">        IndexSearcher searcher = <span class="keyword">new</span> IndexSearcher(reader);</div><div class="line">        </div><div class="line">        <span class="comment">//创建一个简单查询对象，通过指定域和特定值查询文档</span></div><div class="line">        Query query = <span class="keyword">new</span> TermQuery(<span class="keyword">new</span> Term(<span class="string">“filename”</span>, <span class="string">“检索”</span>));</div><div class="line">        <span class="comment">//TopDocs 类是一个简单的指针容器，指针一般指向前N个排名的搜索结果</span></div><div class="line">        TopDocs docs = searcher.search(query, <span class="number">10</span>);</div><div class="line">        <span class="keyword">int</span> total = docs.totalHits;</div><div class="line">        ScoreDoc [] scoreDocs = docs.scoreDocs;</div><div class="line">        System.out.println(<span class="string">“搜索得到的文档数：”</span>+total);</div><div class="line">        <span class="keyword">for</span> (ScoreDoc scoreDoc : scoreDocs) &#123;</div><div class="line">            </div><div class="line">            Document doc = searcher.doc(scoreDoc.doc);</div><div class="line">            display(doc);</div><div class="line">            </div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    <span class="comment">/<strong></strong></span></div><div class="line">     <em> 打印输出文档</em></div><div class="line">      <span class="doctag">@param</span> doc</div><div class="line">     <em>/</em></div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">display</span><span class="params">(Document doc)</span></span>&#123;</div><div class="line">        String filename = doc.get(<span class="string">“filename”</span>);</div><div class="line">        String filePath = doc.get(<span class="string">“filePath”</span>);</div><div class="line">        Long fileSize = <span class="keyword">new</span> Long(doc.get(<span class="string">“fileSize”</span>));</div><div class="line">        </div><div class="line">        System.out.println(<span class="string">“filename:”</span>+filename);</div><div class="line">        System.out.println(<span class="string">“filePath:”</span>+filePath);</div><div class="line">        System.out.println(<span class="string">“fileSize:”</span>+fileSize);</div><div class="line">        System.out.println(<span class="string">“<strong><strong><strong><strong><strong>**</strong></strong></strong></strong></strong>“</span>);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>

<h4 id="实例三（Query的子类查询）"><a href="#实例三（Query的子类查询）" class="headerlink" title="实例三（Query的子类查询）"></a><a href="#实例三（Query的子类查询）" title="实例三（Query的子类查询）"></a>实例三（Query的子类查询）</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> com.eregg.lucene.search;</div><div class="line"></div><div class="line"><span class="keyword">import</span> java.io.File;</div><div class="line"><span class="keyword">import</span> java.io.IOException;</div><div class="line"></div><div class="line"><span class="keyword">import</span> org.apache.lucene.document.Document;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.index.DirectoryReader;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.index.IndexReader;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.index.Term;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.search.BooleanClause.Occur;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.search.BooleanQuery;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.search.IndexSearcher;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.search.MatchAllDocsQuery;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.search.NumericRangeQuery;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.search.Query;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.search.ScoreDoc;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.search.TermQuery;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.search.TermRangeQuery;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.search.TopDocs;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.store.FSDirectory;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.util.BytesRef;</div><div class="line"><span class="keyword">import</span> org.junit.After;</div><div class="line"><span class="keyword">import</span> org.junit.Before;</div><div class="line"><span class="keyword">import</span> org.junit.Test;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">IndexSearchAdvance</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="keyword">private</span> IndexSearcher indexSearcher;</div><div class="line">    <span class="meta">@Before</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">()</span> <span class="keyword">throws</span> Exception</span>&#123;</div><div class="line">        </div><div class="line">        IndexReader reader = DirectoryReader.open(FSDirectory.open(<span class="keyword">new</span> File(<span class="string">“/tmp/lucene/indexdata”</span>)));</div><div class="line">        <span class="keyword">this</span>.indexSearcher = <span class="keyword">new</span> IndexSearcher(reader);</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    <span class="comment">/<strong></strong></span></div><div class="line">     <em> Term 是最基本的查询方式，域：词 </em></div><div class="line">      <span class="doctag">@throws</span> Exception </div><div class="line">     */</div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testTerm</span><span class="params">()</span> <span class="keyword">throws</span> Exception</span>&#123;</div><div class="line">        Query query = <span class="keyword">new</span> TermQuery(<span class="keyword">new</span> Term(<span class="string">“filename”</span>, <span class="string">“lucene”</span>));</div><div class="line">        </div><div class="line">        search(query);</div><div class="line">        </div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="comment">/</span></div><div class="line">     <em> 匹配全部查询</em></div><div class="line">      <span class="doctag">@throws</span> Exception</div><div class="line">     <em>/</em></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testMatchAllDocsQuery</span><span class="params">()</span> <span class="keyword">throws</span> Exception</span>&#123;</div><div class="line">        Query query = <span class="keyword">null</span>;</div><div class="line">        </div><div class="line">        <span class="comment">//1.匹配全部查询</span></div><div class="line">        query = <span class="keyword">new</span> MatchAllDocsQuery();</div><div class="line">        </div><div class="line">        <span class="keyword">this</span>.search(query);</div><div class="line">        </div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    <span class="comment">/**</span></div><div class="line">      指定项范围内搜索</div><div class="line">     <em> <span class="doctag">@throws</span> Exception</em></div><div class="line">     /</div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testTermRangeQuery</span><span class="params">()</span> <span class="keyword">throws</span> Exception</span>&#123;</div><div class="line">        Query query = <span class="keyword">null</span>;</div><div class="line"></div><div class="line">        <span class="comment">//2.指定项范围内搜索</span></div><div class="line">        query = <span class="keyword">new</span> TermRangeQuery(<span class="string">“filename”</span>, <span class="keyword">new</span> BytesRef(<span class="string">“a”</span>.getBytes()), <span class="keyword">new</span> BytesRef(<span class="string">“b”</span>.getBytes()), <span class="keyword">true</span>, <span class="keyword">true</span>);</div><div class="line">        </div><div class="line">        <span class="keyword">this</span>.search(query);</div><div class="line">        </div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    <span class="comment">/<strong></strong></span></div><div class="line">     <em> 指定数字范围查询</em></div><div class="line">      <span class="doctag">@throws</span> Exception</div><div class="line">     */</div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testNumericRangeQuery</span><span class="params">()</span> <span class="keyword">throws</span> Exception</span>&#123;</div><div class="line">        Query query = <span class="keyword">null</span>;</div><div class="line">        </div><div class="line">        <span class="comment">//3.指定数字范围查询</span></div><div class="line">        query = NumericRangeQuery.newLongRange(<span class="string">“fileSize”</span>, <span class="number">0l</span>, <span class="number">1000l</span>, <span class="keyword">true</span>, <span class="keyword">true</span>);</div><div class="line"></div><div class="line">        <span class="keyword">this</span>.search(query);</div><div class="line">        </div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    <span class="comment">/</span></div><div class="line">     <em> 组合查询（多条件组合查询）</em></div><div class="line">      <span class="doctag">@throws</span> Exception</div><div class="line">     <em>/</em></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testBooleanQuery</span><span class="params">()</span> <span class="keyword">throws</span> Exception</span>&#123;</div><div class="line">        Query query = <span class="keyword">null</span>;</div><div class="line">        </div><div class="line">        <span class="comment">//4.组合查询（多条件组合查询）</span></div><div class="line">        TermQuery query1 = <span class="keyword">new</span> TermQuery(<span class="keyword">new</span> Term(<span class="string">“filename”</span>, <span class="string">“apache”</span>));</div><div class="line">        NumericRangeQuery&lt;Long&gt; query2 = NumericRangeQuery.newLongRange(<span class="string">“fileSize”</span>, <span class="number">0l</span>, <span class="number">1000l</span>, <span class="keyword">true</span>, <span class="keyword">true</span>);</div><div class="line">        BooleanQuery booleanQuery = <span class="keyword">new</span> BooleanQuery();</div><div class="line">        <span class="comment">//Occur.MUST:必须满足，Occur.MUST_NOT:必须不满足, Occur.SHOULD:或者的关系</span></div><div class="line">        booleanQuery.add(query1, Occur.MUST);</div><div class="line">        booleanQuery.add(query2, Occur.MUST);</div><div class="line">        query = booleanQuery;</div><div class="line"></div><div class="line">        <span class="keyword">this</span>.search(query);</div><div class="line">        </div><div class="line">    &#125;</div><div class="line"></div><div class="line">    </div><div class="line">    <span class="comment">/**</span></div><div class="line">      该方法分装了查询与输出的功能</div><div class="line">     <em> <span class="doctag">@param</span> query</em></div><div class="line">      <span class="doctag">@throws</span> IOException</div><div class="line">     <em>/</em></div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">search</span><span class="params">(Query query)</span> <span class="keyword">throws</span> IOException </span>&#123;</div><div class="line">        System.out.println(<span class="string">“查询条件：”</span>+query);</div><div class="line">        </div><div class="line">        <span class="comment">//TopDocs主要分装了两个字段，总命中数，文档ID</span></div><div class="line">        TopDocs topDocs = indexSearcher.search(query, <span class="number">10</span>);</div><div class="line">        <span class="keyword">int</span> total = topDocs.totalHits;</div><div class="line">        ScoreDoc[] scoreDocs = topDocs.scoreDocs;</div><div class="line">        System.out.println(<span class="string">“查询到的结果数：”</span>+total);</div><div class="line">        <span class="keyword">for</span> (ScoreDoc scoreDoc : scoreDocs) &#123;</div><div class="line">            Document doc = <span class="keyword">this</span>.indexSearcher.doc(scoreDoc.doc);</div><div class="line">            </div><div class="line">            System.out.println(<span class="string">“filename:”</span>+doc.get(<span class="string">“filename”</span>));</div><div class="line">            System.out.println(<span class="string">“filePath:”</span>+doc.get(<span class="string">“filePath”</span>));</div><div class="line">            System.out.println(<span class="string">“fileContent:”</span>+doc.get(<span class="string">“fileContent”</span>));</div><div class="line">            System.out.println(<span class="string">“fileSize:”</span>+doc.get(<span class="string">“fileSize”</span>));</div><div class="line">            </div><div class="line">            System.out.println(<span class="string">“<strong><strong><strong><em>*</em></strong></strong></strong>“</span>);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    <span class="meta">@After</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">destroy</span><span class="params">()</span></span>&#123;</div><div class="line">        </div><div class="line">    &#125;</div><div class="line">    </div><div class="line">&#125;</div></pre></td></tr></table></figure>

<h4 id="实例四（IndexQueryParser-查询解析器）"><a href="#实例四（IndexQueryParser-查询解析器）" class="headerlink" title="实例四（IndexQueryParser 查询解析器）"></a><a href="#实例四（IndexQueryParser-查询解析器）" title="实例四（IndexQueryParser 查询解析器）"></a>实例四（IndexQueryParser 查询解析器）</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> com.eregg.lucene.queryparser;</div><div class="line"></div><div class="line"><span class="keyword">import</span> java.io.File;</div><div class="line"><span class="keyword">import</span> java.io.IOException;</div><div class="line"><span class="keyword">import</span> java.util.HashMap;</div><div class="line"><span class="keyword">import</span> java.util.Map;</div><div class="line"></div><div class="line"><span class="keyword">import</span> org.apache.lucene.document.Document;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.index.DirectoryReader;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.index.IndexReader;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.queryparser.classic.MultiFieldQueryParser;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.queryparser.classic.QueryParser;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.search.IndexSearcher;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.search.Query;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.search.ScoreDoc;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.search.TopDocs;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.store.Directory;</div><div class="line"><span class="keyword">import</span> org.apache.lucene.store.FSDirectory;</div><div class="line"><span class="keyword">import</span> org.junit.After;</div><div class="line"><span class="keyword">import</span> org.junit.Before;</div><div class="line"><span class="keyword">import</span> org.junit.Test;</div><div class="line"><span class="keyword">import</span> org.wltea.analyzer.lucene.IKAnalyzer;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">IndexQueryParser</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="keyword">private</span> IndexSearcher indexSearcher;</div><div class="line">    </div><div class="line">    <span class="meta">@Before</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">()</span> <span class="keyword">throws</span> Exception</span>&#123;</div><div class="line">        Directory directory = FSDirectory.open(<span class="keyword">new</span> File(<span class="string">“/tmp/lucene/indexdata”</span>)); </div><div class="line">        IndexReader reader = DirectoryReader.open(directory); </div><div class="line">        <span class="keyword">this</span>.indexSearcher = <span class="keyword">new</span> IndexSearcher(reader);</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    <span class="comment">/<strong></strong></span></div><div class="line">     <em> 使用查询解析器 生成 复杂Query,（将查询条件，通过分词器分词，然后组合条件）</em></div><div class="line">      <span class="doctag">@throws</span> Exception </div><div class="line">     */</div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testQueryParser</span><span class="params">()</span> <span class="keyword">throws</span> Exception</span>&#123;</div><div class="line">        <span class="comment">/</span></div><div class="line">         <em> 参数一：默认搜索域，参数二：分词器</em></div><div class="line">         /</div><div class="line">        QueryParser queryParser = <span class="keyword">new</span> QueryParser(<span class="string">“filename”</span>, <span class="keyword">new</span> IKAnalyzer());</div><div class="line">        <span class="comment">//parse 中可以不设置域，默认使用创建QueryParser时，指定的默认域（filename）</span></div><div class="line">        Query query = queryParser.parse(<span class="string">“fileContent:全文检索”</span>);</div><div class="line">        <span class="keyword">this</span>.search(query);</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    <span class="comment">/<strong></strong></span></div><div class="line">     <em> 多个默认域，查询解析器</em></div><div class="line">      <span class="doctag">@throws</span> Exception </div><div class="line">     */</div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testMultiFieldQueryParser</span><span class="params">()</span> <span class="keyword">throws</span> Exception</span>&#123;</div><div class="line">        String [] fields = &#123;<span class="string">“filename”</span>, <span class="string">“fileContent”</span>&#125;;</div><div class="line">        <span class="comment">//参数一：默认搜索域数组（可以指定多个默认搜索域） 参数2：解析查询短语的分词器</span></div><div class="line">        MultiFieldQueryParser multiFieldQueryParser = <span class="keyword">new</span> MultiFieldQueryParser(fields, <span class="keyword">new</span> IKAnalyzer());</div><div class="line">        Query query = multiFieldQueryParser.parse(<span class="string">“lucene is good”</span>);</div><div class="line">        </div><div class="line">        <span class="keyword">this</span>.search(query);</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    <span class="comment">/</span></div><div class="line">     <em> 多个默认域，查询解析器,设置域的权重，从而影响排序结果</em></div><div class="line">      <span class="doctag">@throws</span> Exception </div><div class="line">     <em>/</em></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testMultiFieldQueryParserBoosts</span><span class="params">()</span> <span class="keyword">throws</span> Exception</span>&#123;</div><div class="line">        String [] fields = &#123;<span class="string">“filename”</span>, <span class="string">“fileContent”</span>&#125;;</div><div class="line">        Map&lt;String, Float&gt; boosts = <span class="keyword">new</span> HashMap&lt;String, Float&gt;();</div><div class="line">        boosts.put(<span class="string">“filename”</span>, <span class="number">1f</span>);</div><div class="line">        boosts.put(<span class="string">“fileContent”</span>, <span class="number">10f</span>);</div><div class="line">        <span class="comment">//参数一：默认搜索域数组（可以指定多个默认搜索域） 参数2：解析查询短语的分词器,参数3：设置域的权重值</span></div><div class="line">        MultiFieldQueryParser multiFieldQueryParser = <span class="keyword">new</span> MultiFieldQueryParser(fields, <span class="keyword">new</span> IKAnalyzer(), boosts);</div><div class="line">        Query query = multiFieldQueryParser.parse(<span class="string">“lucene is good”</span>);</div><div class="line">        </div><div class="line">        <span class="keyword">this</span>.search(query);</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    </div><div class="line">    <span class="comment">/**</span></div><div class="line">      该方法分装了查询与输出的功能</div><div class="line">     <em> <span class="doctag">@param</span> query</em></div><div class="line">      <span class="doctag">@throws</span> IOException</div><div class="line">     <em>/</em></div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">search</span><span class="params">(Query query)</span> <span class="keyword">throws</span> IOException </span>&#123;</div><div class="line">        System.out.println(<span class="string">“查询条件：”</span>+query);</div><div class="line">        </div><div class="line">        <span class="comment">//TopDocs主要分装了两个字段，总命中数，文档ID</span></div><div class="line">        TopDocs topDocs = indexSearcher.search(query, <span class="number">10</span>);</div><div class="line">        <span class="keyword">int</span> total = topDocs.totalHits;</div><div class="line">        ScoreDoc[] scoreDocs = topDocs.scoreDocs;</div><div class="line">        </div><div class="line">        System.out.println(<span class="string">“查询到的结果数：”</span>+total);</div><div class="line">        <span class="keyword">for</span> (ScoreDoc scoreDoc : scoreDocs) &#123;</div><div class="line">            Document doc = <span class="keyword">this</span>.indexSearcher.doc(scoreDoc.doc);</div><div class="line">            System.out.println(<span class="string">“得分:”</span>+scoreDoc.score);</div><div class="line">            System.out.println(<span class="string">“filename:”</span>+doc.get(<span class="string">“filename”</span>));</div><div class="line">            System.out.println(<span class="string">“filePath:”</span>+doc.get(<span class="string">“filePath”</span>));</div><div class="line">            System.out.println(<span class="string">“fileContent:”</span>+doc.get(<span class="string">“fileContent”</span>));</div><div class="line">            System.out.println(<span class="string">“fileSize:”</span>+doc.get(<span class="string">“fileSize”</span>));</div><div class="line">            </div><div class="line">            System.out.println(<span class="string">“<strong><strong><strong><em>*</em></strong></strong></strong>“</span>);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="meta">@After</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">destory</span><span class="params">()</span></span>&#123;</div><div class="line">        </div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/计算机/" rel="tag"># 计算机</a>
          
            <a href="/tags/全文检索/" rel="tag"># 全文检索</a>
          
            <a href="/tags/lucene/" rel="tag"># lucene</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/11/22/zookeeper/" rel="next" title="zookeeper">
                <i class="fa fa-chevron-left"></i> zookeeper
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          
            <img class="site-author-image" itemprop="image"
              src="/uploads/avatar.jpg"
              alt="eregg" />
          
            <p class="site-author-name" itemprop="name">eregg</p>
            <p class="site-description motion-element" itemprop="description">青，取之于蓝，而青于蓝</p>
        </div>

        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
            
              <a href="/archives/">
            
                <span class="site-state-item-count">10</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">31</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/eregg" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>GitHub</a>
              </span>
            
              <span class="links-of-author-item">
                <a href="mailto:ereggmail@163.com" target="_blank" title="E-Mail">
                  
                    <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#LUCENE是什么"><span class="nav-number">1.</span> <span class="nav-text">LUCENE是什么</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Lucene-和搜索程序组建"><span class="nav-number">2.</span> <span class="nav-text">Lucene 和搜索程序组建</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#索引过程的核心类"><span class="nav-number">3.</span> <span class="nav-text">索引过程的核心类</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#IndexWriter"><span class="nav-number">3.1.</span> <span class="nav-text">IndexWriter</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#Directory"><span class="nav-number">3.2.</span> <span class="nav-text">Directory</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#Analyzer"><span class="nav-number">3.3.</span> <span class="nav-text">Analyzer</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#Document"><span class="nav-number">3.4.</span> <span class="nav-text">Document</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#Field"><span class="nav-number">3.5.</span> <span class="nav-text">Field</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#搜索过程的核心类"><span class="nav-number">4.</span> <span class="nav-text">搜索过程的核心类</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#IndexSearcher"><span class="nav-number">4.1.</span> <span class="nav-text">IndexSearcher</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#Term"><span class="nav-number">4.2.</span> <span class="nav-text">Term</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#Query"><span class="nav-number">4.3.</span> <span class="nav-text">Query</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#TermQuery-通过项进行搜索"><span class="nav-number">4.3.1.</span> <span class="nav-text">TermQuery 通过项进行搜索</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#TermRangeQuery-在指定的项范围内搜索"><span class="nav-number">4.3.2.</span> <span class="nav-text">TermRangeQuery 在指定的项范围内搜索</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#NumericRangeQuery-在指定的数字范围内搜索"><span class="nav-number">4.3.3.</span> <span class="nav-text">NumericRangeQuery 在指定的数字范围内搜索</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#PrefixQuery-通过字符串搜索"><span class="nav-number">4.3.4.</span> <span class="nav-text">PrefixQuery 通过字符串搜索</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#FuzzyQuery-搜索类似项"><span class="nav-number">4.3.5.</span> <span class="nav-text">FuzzyQuery 搜索类似项</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#MatchAllDocsQuery-匹配所有文档"><span class="nav-number">4.3.6.</span> <span class="nav-text">MatchAllDocsQuery 匹配所有文档</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#WildcardQuery-通配符查询"><span class="nav-number">4.3.7.</span> <span class="nav-text">WildcardQuery 通配符查询</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#BooleanQuery-组合查询"><span class="nav-number">4.3.8.</span> <span class="nav-text">BooleanQuery 组合查询</span></a></li></ol></li><li class="nav-item nav-level-5"><a class="nav-link" href="#QueryParser-解析查询表达式"><span class="nav-number">4.4.</span> <span class="nav-text">QueryParser 解析查询表达式</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#MultiFieldQueryParser-一次性搜索多个域"><span class="nav-number">4.4.1.</span> <span class="nav-text">MultiFieldQueryParser 一次性搜索多个域</span></a></li></ol></li><li class="nav-item nav-level-5"><a class="nav-link" href="#TopDocs"><span class="nav-number">4.5.</span> <span class="nav-text">TopDocs</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例一（建立索引）"><span class="nav-number">5.</span> <span class="nav-text">实例一（建立索引）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例二（搜索索引）"><span class="nav-number">6.</span> <span class="nav-text">实例二（搜索索引）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例三（Query的子类查询）"><span class="nav-number">7.</span> <span class="nav-text">实例三（Query的子类查询）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例四（IndexQueryParser-查询解析器）"><span class="nav-number">8.</span> <span class="nav-text">实例四（IndexQueryParser 查询解析器）</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2015 &mdash; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">eregg</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动</div>

  <span class="post-meta-divider">|</span>

  <div class="theme-info">主题 &mdash; <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.2</div>


        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>

  
  <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.2"></script>



  
  

  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.2"></script>


  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  








  





  

  

  

  

  

  

</body>
</html>
